package com.qlm.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.qlm.entity.Exchangerecord;
import com.qlm.entity.Gift;
import com.qlm.service.IAwardService;
import com.qlm.tools.Common;
import com.qlm.tools.DateUtils;
import com.qlm.view.ArwardView;
import com.qlm.view.HBView;

public class ArwardServiceImpl implements IAwardService{

	@Override
	public String djApply(String aoData) {
		//将参数转换成jsonArray类型
		JSONArray jsonarray = JSONArray.parseArray(aoData);
		JSONObject obj = (JSONObject) jsonarray.get(0);
		// 传入的查询参数
		String sSearch = obj.get("sSearch").toString();
		
		List<Object> arList = new ArrayList<Object>();
		String lgSql = "SELECT e.id,e.name_,e.mobile,e.gift_id,e.create_time,e.coupon,e.addr,e.status,u.nick_name,u.head_pic from t_jf_exchange e,t_user u  where u.openid = e.openid and status = 0 and hbmoney is null   ";
		lgSql = lgSql +" order by  create_time desc";
		List<Record> lgList = Db.find(lgSql);
		int i = 1;
		for (Record logistics : lgList) {
			Integer gift_id = logistics.getInt("gift_id");
			if(gift_id == 99999999){//实物
				continue;
			}else if(gift_id == 8888888){
				continue;
			}
			ArwardView av = new ArwardView();
			av.setId(logistics.getInt("id")+"");
			av.setSeq(i+"");
			av.setContactName(logistics.getStr("name_"));
			av.setMobile(logistics.getStr("mobile"));
			String coupon = logistics.get("coupon");		
			String giftname = gift_id==null?"":(Gift.Dao.findById(gift_id)==null?"":Gift.Dao.findById(gift_id).get("gift_name"));
			if(Common.isEmptyString(coupon)){				
				giftname += "("+coupon+")";
			}
			av.setGiftName(giftname);
			av.setHeadPic(logistics.get("head_pic"));
			av.setNickName(logistics.get("nick_name"));
			av.setUpdatetime(DateUtils.formatString(
					 logistics.get("create_time"), DateUtils.PATTEN_HMS));
			av.setAddress(logistics.getStr("addr"));
			av.setStatus(logistics.getInt("status"));
			arList.add(av);
			i++;
		}
		 // 调用datatable分页查询方法，返回json对象字符串
		 return Common.dataTableSearch(aoData, arList);
	}

	@Override
	public String djRecord(String aoData) {
		//将参数转换成jsonArray类型
		JSONArray jsonarray = JSONArray.parseArray(aoData);
		JSONObject obj = (JSONObject) jsonarray.get(0);
		// 传入的查询参数
		String sSearch = obj.get("sSearch").toString();
		
		List<Object> arList = new ArrayList<Object>();
		String lgSql = "SELECT e.id,e.name_,e.mobile,e.gift_id,e.logistics_name,e.coupon,e.logistics_num,e.send_time,e.create_time,e.addr,e.status,u.nick_name,u.head_pic from t_jf_exchange e,t_user u  where u.openid = e.openid and status != 0 and hbmoney is null  ";
		lgSql = lgSql +" order by  create_time desc";
		List<Record> lgList = Db.find(lgSql);
		int i = 1;
		for (Record logistics : lgList) {
			Integer gift_id = logistics.getInt("gift_id");
			if(gift_id == 99999999){//实物
				continue;
			}else if(gift_id == 8888888){
				continue;
			}
			ArwardView av = new ArwardView();
			av.setId(logistics.getInt("id")+"");
			av.setSeq(i+"");
			av.setContactName(logistics.get("name_"));
			av.setMobile(logistics.get("mobile"));
			String coupon = logistics.get("coupon");
			String giftname = gift_id==null?"":(Gift.Dao.findById(gift_id)==null?"":Gift.Dao.findById(gift_id).get("gift_name"));
			if(Common.isEmptyString(coupon)){				
				giftname += "("+coupon+")";
			}
			av.setGiftName(giftname);
			av.setHeadPic(logistics.get("head_pic"));
			av.setNickName(logistics.get("nick_name"));
			av.setUpdatetime(DateUtils.formatString(
					 logistics.get("create_time"), DateUtils.PATTEN_HMS));
			av.setReceiveTime(DateUtils.formatString(
					 logistics.get("send_time"), DateUtils.PATTEN_HMS));
			av.setLogisticsNum(logistics.get("logistics_num"));
			av.setLogisticsName(logistics.get("logistics_name"));
			av.setAddress(logistics.get("addr"));
			av.setStatus(logistics.getInt("status"));
			arList.add(av);
			i++;
		}
		// 调用datatable分页查询方法，返回json对象字符串
		return Common.dataTableSearch(aoData, arList);
	}

	@Override
	@Before(Tx.class)
	public boolean addLogisticsInformation(String logisticsName,String logisticsNum,Integer id) {
		// 更新兑奖记录
		if(Common.isNullOrEmpty(id) || !Common.isEmptyString(logisticsNum) || !Common.isEmptyString(logisticsName)){
			return false;
		}
		new Exchangerecord().findById(id).set("status", 1)
				.set("send_time", new Date())
				.set("logistics_name", logisticsName)
				.set("logistics_num", logisticsNum).update();
		// 即时发送指定的微信用户

		return true;
	}

	@Override
	public String hbDhRecord(String aoData) {
		//将参数转换成jsonArray类型
		JSONArray jsonarray = JSONArray.parseArray(aoData);
		JSONObject obj = (JSONObject) jsonarray.get(0);
		// 传入的查询参数
		String sSearch = obj.get("sSearch").toString();
		
		List<Object> arList = new ArrayList<Object>();
		String lgSql = "SELECT u.nick_name,u.head_pic,e.create_time,e.hbmoney from t_user u,t_jf_exchange e where u.openid = e.openid and  e.hbmoney is not null  ";
		lgSql = lgSql +" order by  create_time desc";
		List<Record> lgList = Db.find(lgSql);
		int i = 1;
		for (Record logistics : lgList) {
			HBView av = new HBView();
			av.setSeq(i+"");
			av.setHbmoney(logistics.getInt("hbmoney").intValue()/new Double(100));
			av.setCreate_time(DateUtils.formatString(
					 logistics.get("create_time"), DateUtils.PATTEN_HMS));
			av.setNick_name(logistics.get("nick_name"));
			av.setHead_pic(logistics.get("head_pic")==null?"":logistics.get("head_pic"));
			arList.add(av);
			i++;
		}
		// 调用datatable分页查询方法，返回json对象字符串
		return Common.dataTableSearch(aoData, arList);
	}

}
